<?php
/**
 * File:          KValidatorElementOption
 * Description:   -
 * Author:        Kovac "Kovacou'" Alexandre (www.kovacou.fr)
 * Last Update:   14/04/2011
 */

   abstract class KValidator_Element_Option extends KValidatorElement {
      protected   $multiple = false,
                  $unique   = true;
      
      
      public function multiple ($bool) {
         $this->multiple = (bool) $bool;
         
         return $this;
      }
      
      public function unique ($bool) {
         $this->unique = (bool) $bool;
         
         return $this;
      }
      
      public function value () {
         $param = $this->_parameter("value");
         $param->values = array_map('trim', array_unique (func_get_args ()));
         
         return $this;         
      }
      
      
      public function int ($min = null, $max = null) {
         if (true === isset($min))
            $min = filter_var ($min, FILTER_SANITIZE_NUMBER_INT);

         if (true === isset($max))
            $max = filter_var ($max, FILTER_SANITIZE_NUMBER_INT);

         if (KV_DEBUG && true === isset($min, $max) && (($inc && $min > $max) || (!$inc && $min >= $max)))
            throw new Exception ("KValidatorElementOption::int() - Vous avez défini un interval avec un minimum supérieur à un maximum.");

         $param = $this->_parameter ("int");
         $param->min = $min;
         $param->max = $max;

         return $this;
      }
      
      
      public function valid () {
         if (true === is_array ($this->value)) {
            if (true === $this->unique)
               $value = array_unique ($this->value);
            if (true === $this->trim)
               $value = array_map ("trim", $this->value);
         }
         elseif (true === $this->trim)
            $value = trim ($this->value);
         
         if (true === $this->required && false === $this->isset)
            $error = "Vous devez sélectionner au moins une option.";
         else
            foreach ($this->params as &$param) {
               $error = $param->valid ($this->value, array ($this->uniqid));

               if (isset ($error))
                  break;
            }
         
         if (isset ($error)) {
            KValidator::load ($this->uniqid)->setError ($this->name, $error);
            return false;
         }
         
         KValidator::load ($this->uniqid)->setValue ($this->name, $this->value);
                 
         return true;
      }
      
      
      protected function _parameter ($param) {
         return parent::_parameter("Option", $param);
      }
   }
 
/**
 * End
 * Of
 * File
 */
?>